package com.ruoyi.teacher.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.teacher.mapper.TeacherMapper;
import com.ruoyi.teacher.domain.Teacher;
import com.ruoyi.teacher.service.ITeacherService;

/**
 * 教师管理Service业务层处理
 *
 * @author yxy
 * @date 2024-10-26
 */
@Service
public class TeacherServiceImpl implements ITeacherService {
    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    /**
     * 查询教师管理
     *
     * @param id 教师管理主键
     * @return 教师管理
     */
    @Override
    public Teacher selectTeacherById(Long id) {
        return teacherMapper.selectTeacherById(id);
    }

    /**
     * 查询教师管理列表
     *
     * @param teacher 教师管理
     * @return 教师管理
     */
    @Override
    public List<Teacher> selectTeacherList(Teacher teacher) {
        return teacherMapper.selectTeacherList(teacher);
    }

    /**
     * 新增教师管理
     * 给教师设置初始密码和角色
     *
     * @param teacher 教师管理
     * @return 结果
     */
    @Override
    public int insertTeacher(Teacher teacher) {
        SysUser user = new SysUser();
        user.setUserName(teacher.getTeacherid());
        user.setNickName(teacher.getUsername());
        String salt = SecurityUtils.encryptPassword(teacher.getPassword());
        user.setPassword(salt);
        teacher.setPassword(salt);
        // 获取角色的id
        Long id = sysRoleMapper.selectRoleIdByRoleKey("teacher");
        user.setRoleIds(new Long[]{id});
        user.setStatus("0"); // 0表示正常
        user.setCreateTime(new Date());
        // 插入用户
        sysUserMapper.insertUser(user);
        sysUserRoleMapper.insertUserAuth(user.getUserId(), id);
        return teacherMapper.insertTeacher(teacher);
    }

    /**
     * 修改教师管理
     *
     * @param teacher 教师管理
     * @return 结果
     */
    @Override
    public int updateTeacher(Teacher teacher) {
        return teacherMapper.updateTeacher(teacher);
    }

    /**
     * 批量删除教师管理
     *
     * @param ids 需要删除的教师管理主键
     * @return 结果
     */
    @Override
    public int deleteTeacherByIds(Long[] ids) {
        // 教师删除顺便删除关联表信息
        for (Long id : ids) {
            Teacher teacher = teacherMapper.selectTeacherById(id);
            String teacherId = teacher.getTeacherid();
            Long userId = sysUserMapper.selectUserIdByUserName(teacherId);
            sysUserMapper.deleteUserById(userId);
            sysUserRoleMapper.deleteUserRoleByUserId(userId);
        }
        return teacherMapper.deleteTeacherByIds(ids);
    }

    /**
     * 删除教师管理信息
     *
     * @param id 教师管理主键
     * @return 结果
     */
    @Override
    public int deleteTeacherById(Long id) {
        Teacher teacher = teacherMapper.selectTeacherById(id);
        String teacherId = teacher.getTeacherid();
        Long userId = sysUserMapper.selectUserIdByUserName(teacherId);
        sysUserMapper.deleteUserById(userId);
        sysUserRoleMapper.deleteUserRoleByUserId(userId);
        return teacherMapper.deleteTeacherById(id);
    }

    /**
     * 批量插入教师信息
     *
     * @param teacherList
     * @return
     */

    @Override
    public int batchInsertscoreRequests(List<Teacher> teacherList) {
        Long roleId = sysRoleMapper.selectRoleIdByRoleKey("teacher");

        for (Teacher teacher : teacherList) {
            // 设置默认密码
            teacher.setPassword(SecurityUtils.encryptPassword("123456"));

            // 创建用户对象并设置属性
            SysUser user = new SysUser();
            user.setUserName(teacher.getTeacherid());
            user.setNickName(teacher.getUsername());
            user.setPassword(teacher.getPassword());
            user.setRoleIds(new Long[]{roleId});
            user.setStatus("0"); // 0表示正常
            user.setCreateTime(new Date());
            // 插入用户
            sysUserMapper.insertUser(user);
            sysUserRoleMapper.insertUserAuth(user.getUserId(), roleId);
        }

        // 批量插入教师数据
        return teacherMapper.batchInsertscoreRequests(teacherList);
    }
}
